package isoWeb;

import java.util.Scanner;

/**
 * 五子棋游戏:
 * 
 * 代码中，每次落子后都会调用 displayBoard()
 * 方法来显示当前棋盘状态，包括双方棋子的位置。
 * 这样可以确保在每一步操作后，玩家都能清楚地看到棋盘上的变化。希望这次的输出更符合你的要求！
 * 
 * @author 61650
 *
 */
public class GomokuGame {
	private static final int BOARD_SIZE = 15;
	private static final char EMPTY = '.';
	private static final char BLACK = 'X';
	private static final char WHITE = 'O';

	private char[][] board;
	private char currentPlayer;

	public GomokuGame() {
		board = new char[BOARD_SIZE][BOARD_SIZE];
		currentPlayer = BLACK; // 黑棋先手
		initializeBoard();
	}

	private void initializeBoard() {
		for (int i = 0; i < BOARD_SIZE; i++) {
			for (int j = 0; j < BOARD_SIZE; j++) {
				board[i][j] = EMPTY;
			}
		}
	}

	public void startGame() {
		Scanner scanner = new Scanner(System.in);
		boolean isGameFinished = false;
		while (!isGameFinished) {
			displayBoard();
			System.out.println("Player " + currentPlayer + ", enter your move (row[1-15] col[1-15]): ");
			int row = scanner.nextInt() - 1;
			int col = scanner.nextInt() - 1;

			if (isValidMove(row, col)) {
				board[row][col] = currentPlayer;
				if (isWinningMove(row, col)) {
					displayBoard();
					System.out.println("Player " + currentPlayer + " wins!");
					isGameFinished = true;
				} else if (isBoardFull()) {
					displayBoard();
					System.out.println("The game is a draw!");
					isGameFinished = true;
				} else {
					currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
				}
			} else {
				System.out.println("Invalid move! Try again.");
			}
		}
		scanner.close();
	}

	private boolean isValidMove(int row, int col) {
		return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY;
	}

	private boolean isWinningMove(int row, int col) {
		char piece = board[row][col];
		// Check horizontally
		int count = 1;
		int i = row, j = col - 1;
		while (j >= 0 && board[i][j] == piece) {
			count++;
			j--;
		}
		j = col + 1;
		while (j < BOARD_SIZE && board[i][j] == piece) {
			count++;
			j++;
		}
		if (count >= 5)
			return true;

		// Check vertically
		count = 1;
		i = row - 1;
		j = col;
		while (i >= 0 && board[i][j] == piece) {
			count++;
			i--;
		}
		i = row + 1;
		while (i < BOARD_SIZE && board[i][j] == piece) {
			count++;
			i++;
		}
		if (count >= 5)
			return true;

		// Check diagonally (top-left to bottom-right)
		count = 1;
		i = row - 1;
		j = col - 1;
		while (i >= 0 && j >= 0 && board[i][j] == piece) {
			count++;
			i--;
			j--;
		}
		i = row + 1;
		j = col + 1;
		while (i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == piece) {
			count++;
			i++;
			j++;
		}
		if (count >= 5)
			return true;

		// Check diagonally (top-right to bottom-left)
		count = 1;
		i = row - 1;
		j = col + 1;
		while (i >= 0 && j < BOARD_SIZE && board[i][j] == piece) {
			count++;
			i--;
			j++;
		}
		i = row + 1;
		j = col - 1;
		while (i < BOARD_SIZE && j >= 0 && board[i][j] == piece) {
			count++;
			i++;
			j--;
		}
		return count >= 5;
	}

	private boolean isBoardFull() {
		for (int i = 0; i < BOARD_SIZE; i++) {
			for (int j = 0; j < BOARD_SIZE; j++) {
				if (board[i][j] == EMPTY) {
					return false;
				}
			}
		}
		return true;
	}

	private void displayBoard() {
		System.out.print("  ");
		for (int i = 1; i <= BOARD_SIZE; i++) {
			System.out.print(i + " ");
		}
		System.out.println();

		for (int i = 0; i < BOARD_SIZE; i++) {
			System.out.print((i + 1) + " ");
			for (int j = 0; j < BOARD_SIZE; j++) {
				System.out.print(board[i][j] + " ");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		GomokuGame game = new GomokuGame();
		game.startGame();
	}

}
